#include <stdio.h>
#include <strings.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <assert.h>
#include <sys/epoll.h>

#include "filedispat.h"
#include "like.h"
#include "socket.h"
#include "epoll.h"

int register_operation(int connfd,char *buff,user_data data,userinfo *head)
{
	int ret,val,fd;
	bzero((user_data*)&data,sizeof(user_data));
	sscanf(buff,"%d %d %s",&val,&fd,data.name);
	data.mark=val;data.fd=connfd;

	ret = ergodic_list(data.name,head);
	if(ret==-1)
		return -1;
	
	ret = Insert_like(data,head);
	if(ret == -1)
		perr_exit("malloc end error");

	return 1;
	
}

void send_socket(int sockfd,userinfo *head)
{
	char buf[SOCK_BUF_SIZE];
	userinfo *p;
	p=head->next;
	bzero((char*)buf,sizeof(buf));
	sprintf(buf,"%d%s",REG_INFO,"------registration complete--------OK!!!\n ");
	sprintf(buf+strlen(buf),"%s","The current online user has\n");
	while(p!=NULL){
		sprintf(buf+strlen(buf),"######%s######\n",p->user.name);
		p=p->next;
	}
	if(writen(sockfd,buf,SOCK_BUF_SIZE)==-1){
		perror("writen error");
		close(sockfd);
	}
		
}

void send_register_erron(int socket)
{
	char buf[SOCK_BUF_SIZE];
	bzero((char*)buf,sizeof(buf));
	sprintf(buf,"%d%s",REG_INFO,"The user name has been registered, please enter it again\n");
	sprintf(buf+strlen(buf),"%s","!!!!! registration failed !!!!!!\n ");
	//if(send(socket,buf,strlen(buf),0)==-1)
		//perr_exit("send reg");
	if(writen(socket, buf, SOCK_BUF_SIZE)==-1)
	{
		perror("writen reg");
		close(socket);
	}

	return ;
}

int on_line_updata(userinfo *head)
{
	char buf[SOCK_BUF_SIZE];
	userinfo *p;
	p=head->next;
	sprintf(buf,"%d%s",REG_INFO,"------registration complete--------OK!!!\n ");
	sprintf(buf+strlen(buf),"%s","The current online user has\n");
	while(p!=NULL){
		sprintf(buf+strlen(buf),"######%s######\n",p->user.name);
		p=p->next;
	}
	p=head->next;
	while(p!=NULL){
		if(writen(p->user.fd,buf,SOCK_BUF_SIZE)==-1)
			return -1;
		p=p->next;
	}
	return 0;
}

void public_catinfo_post(int sockfd,public_cat_packet packet,char *buf)
{
	int val;
	userinfo *p=head->next;
	memset((public_cat_packet*)&packet,0,sizeof(public_cat_packet));
	sscanf(buf,"%d %[^\n]",&val,packet.buff);
	char *pbuf = (char *)malloc(sizeof(int)+strlen(packet.buff));
	assert(pbuf);
	sprintf(pbuf,"%d%s",CHAT_INFO,packet.buff);
	//printf("%s\n",packet.buff);
	//bzero((char*)buf,sizeof(buf));
	//sprintf(buf,"%s",packet.buff);
	//printf("..............%s...............\n",packet.buff);
	while(p!=NULL){
		if(sockfd!=p->user.fd)
			writen(p->user.fd,pbuf,SOCK_BUF_SIZE);
		//printf("-%d-\n",p->user.fd);
		p=p->next;
	}
	return ;
}

void private_catinfo_post(int sockfd,chat_packet packet,userinfo *head,char *buf)
{
	char buff[SOCK_BUF_SIZE];
	int val,i=0;
	userinfo *p=head->next;
	bzero((chat_packet*)&packet,sizeof(chat_packet));
	sscanf(buf,"%d %s %[^\n]",&val,packet.tar_name,packet.buff);
	//printf("-%s-\n",packet.buff);
	bzero((char*)buff,sizeof(buff));
	//sprintf(buff,"%[^\n]",packet.buff);
	//strncpy(buff,packet.buff,strlen(packet.buff));
	sprintf(buff,"%d%s",CHAT_INFO,packet.buff);
	//printf("%s\n",buff);
	while(p!=NULL){
		if((strcmp(p->user.name,packet.tar_name))==0){
			writen(p->user.fd,buff,SOCK_BUF_SIZE);
			i++;
			}
		p=p->next;
	}
	if(i==0){
		memset(buff,0,strlen(buff));
		sprintf(buff,"%d%s",REG_INFO,"The user does not exist\n");
		writen(sockfd,buff,SOCK_BUF_SIZE);
	}
	return ;
}

void send_file_error(int fd)
{
	int ret;
	char *buf = (char *)malloc(SOCK_BUF_SIZE);
	memset(buf,0,SOCK_BUF_SIZE);
	sprintf(buf,"%d%s",ERR_INFO,"The user does not exist and cannot send files\n");
	ret = writen(fd, buf, SOCK_BUF_SIZE);
	if(ret == -1)
		perror("writen");
	free(buf);
}
